<!--
 * @Description: 液位仪
 * @Author: wangwangwang
 * @Date: 2020-09-21 14:09:06
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-29 15:04:57
-->
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display: flex">
        <div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px">站点</span>
                  <el-select v-model="tableParam.stationId" placeholder="请选择">
                    <el-option v-for="(item,index) in pointList" :key="index" :label="item.name" :value="item.id"></el-option>
                  </el-select>
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">特材成品名称</span>
                  <el-input v-model="tableParam.productName" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">成品型号</span>
                  <el-input v-model="tableParam.productType" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="init()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0" @click="addFromDate('添加')">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px"></i>
                  添加
                </el-button>
              </div>
              <div>

                <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px">
                  <el-link :underline="false">
                    <span class="el-icon--left">
                      <i class="iconfont iconicon_button_filtrate"></i>
                    </span>
                    筛选
                  </el-link>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(item, index) in table" :key="index">
                      <el-checkbox v-model="item.status">{{
                        item.label
                        }}</el-checkbox>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
            <div v-loading="tableLoading" class="card_info" :style="style">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="tableData" tooltip-effect="dark">
                <template v-for="(item, index2) in table">
                  <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="item.label =='操作'?'right':false">
                    <template slot-scope="scope">
                      <span v-if="item.label=='绑定库存仓'">{{`${scope.row.warehouseName}-${scope.row.rawName}-${scope.row.specification}`}}</span>
                      <div v-else-if="item.label == '操作'">
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="updateFromDate(scope.row, '修改' ), productListFun(scope.row.stationId)">
                          修改
                        </el-button>
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='XUQIUJIHUA'))!==-1" class="bottom_one" size="mini" @click="open('addDialog1',scope.row)">
                          需求计划
                        </el-button>
                        <!-- <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='KCPD'))!==-1" class="bottom_one" size="mini" @click="open('addDialog2',scope.row)">
                          库存盘点 // 2021年07月01日15:00:06 产品要求页面屏蔽次功能
                        </el-button>
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='PDJL'))!==-1" class="bottom_one" size="mini" @click="open('addDialog3',scope.row)">
                          盘点历史
                        </el-button> -->
                      </div>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" @size-change="sizeChange" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="480px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="stationId">
            <span slot="label" style="color: #ff0000">站点</span>
            <el-select ref="point" v-model="fromDate.stationId" placeholder="请选择站点" @change="$nextTick(()=>{fromDate.station = $refs.point.selectedLabel}),productListFun(fromDate.stationId),fromDate.rawInventoryWarehouseId = '',fromDate.productId = '';">
              <el-option v-for="(item,index) in pointList" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="name" :rules="[{required: true,message: '请输入液位仪名称',trigger: 'change',}]">
            <span slot="label" style="color: #ff0000">液位仪名称</span>
            <el-input v-model="fromDate.name" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="rawInventoryWarehouseId" :rules="[{required: true,message: '请选择库存仓',trigger: 'change',}]">
            <span slot="label" style="color: #ff0000">绑定库存仓</span>
            <el-select v-model="fromDate.rawInventoryWarehouseId" placeholder="请选择" filterable @change="changeWareHouse">
              <el-option v-for="(item, index) in storeHouseList" :key="index" :label="item.warehouseName+'-'+item.rawName+'-'+item.specification" :value="item.rawWarehouseId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="productId">
            <span slot="label" style="color: #ff0000">特材成品</span>
            <el-select v-model="fromDate.productId" placeholder="请选择" filterable>
              <el-option v-for="(item, index) in productList" :key="index" :label="item.productName+'-'+item.type" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="maxInventory">
            <span slot="label">最大库存量</span>
            <el-input v-model="fromDate.maxInventory" placeholder="请输入" onkeypress='return( /\d|\./.test(String.fromCharCode(event.keyCode)))' @input="fromDate.maxInventory=$regExp.checkNum3(fromDate.maxInventory)" @blur="fromDate.maxInventory=$regExp.blurCheckNum(fromDate.maxInventory)">
              <template slot="append">吨</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="actualInventory">
            <span slot="label">实际库存量</span>
            <el-input v-model="fromDate.actualInventory" disabled placeholder="请输入" clearable>
              <template slot="append">吨</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="safetyInventory">
            <span slot="label">安全库存量</span>
            <el-input v-model="fromDate.safetyInventory" placeholder="请输入" clearable onkeypress='return( /\d|\./.test(String.fromCharCode(event.keyCode)))' @input="fromDate.safetyInventory=$regExp.checkNum3(fromDate.safetyInventory)" @blur="fromDate.safetyInventory=$regExp.blurCheckNum(fromDate.safetyInventory),safetyInventoryFun(fromDate.maxInventory)">
              <template slot="append">吨</template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="pagessubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 需求计划 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog1" width="480px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">需求计划</span>
      <div class="dialoginfo">
        <el-form ref="addDialog1" :rules="lowrules1" :inline="true" :model="low" label-width="110px">
          <el-form-item>
            <span slot="label">特材成品名称：</span>
            <div class="el-input">{{low.productName}}</div>
          </el-form-item>
          <el-form-item>
            <span slot="label">成品型号：</span>
            <div class="el-input">{{low.productType}}</div>
          </el-form-item>
          <el-form-item prop="needNum">
            <span slot="label">需求量</span>
            <el-input v-model="low.needNum" type="Number" placeholder="请输入" clearable>
              <template slot="append">吨</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="needTime">
            <span slot="label">需求时间</span>
            <el-date-picker v-model="low.needTime" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="code">
            <span slot="label">备注</span>
            <el-input v-model="low.remark" placeholder="请填写备注" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('addDialog1')">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="submit1('addDialog1')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 库存盘点 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog2" width="480px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">库存盘点</span>
      <div class="dialoginfo">
        <el-form ref="addDialog2" :rules="lowrules2" :inline="true" :model="low" label-width="110px">
          <el-form-item>
            <span slot="label">特材名称：</span>
            <div class="el-input">{{low.productName}}</div>
          </el-form-item>
          <el-form-item prop="num">
            <span slot="label" style="color: #ff0000">库存盘点量</span>
            <el-input v-model="low.num" placeholder="请输入" clearable>
              <template slot="append">吨</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="checkTime">
            <span slot="label" style="color: #ff0000">盘点时间</span>
            <el-date-picker v-model="low.checkTime" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('addDialog2')">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="submit2('addDialog2')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 盘点历史 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog3" width="680px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">盘点历史</span>
      <div style="height:470px;">
        <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%;height:428px" :data="tableData1" tooltip-effect="dark">
          <el-table-column label="特材名称" align="center" prop="name"> </el-table-column>
          <el-table-column label="盘点库存" align="center" prop="num"> </el-table-column>
          <el-table-column label="盘点人" align="center" prop="createUserName"> </el-table-column>
          <el-table-column label="盘点时间" align="center" prop="checkTime"> </el-table-column>
        </el-table>
        <div class="pagination">
          <div class="top_page_totle">总共{{ total2 }}条</div>
          <el-pagination :current-page.sync="from3.page" :page-size="from3.pageSize" layout="prev, pager, next, jumper" :total="total2" @current-change="getarr3()"></el-pagination>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="reset('addDialog3')">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  saveOrUpdate,
  productList,
  saveneed,
  stockTaking,
  recordsqueryPage,
  getStoreHouse,
} from "@/api/smart/warehouse/LIM.js";
import { getUserAutList, get_simple_organization } from '@/api/common.js';
import { setTimeout } from 'timers';
export default {
  mixins: [mixin],
  data() {
    return {
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      saveOrUpdate: saveOrUpdate,
      /*表格数据*/
      table: [
        { label: "液位仪编号", width: "160", prop: "code", status: true },
        { label: "站点", width: "160", prop: "station", status: true },
        { label: "液位仪名称", width: "160", prop: "name", status: true },
        { label: "绑定库存仓", width: "160", prop: "bindStoreHouse", status: true },
        { label: "特材成品名称", width: "240", prop: "productName", status: true },
        { label: "成品型号", width: "240", prop: "productType", status: true },
        { label: "最大库存量（吨）", width: "120", prop: "maxInventory", status: true },
        { label: "实际库存量（吨）", width: "120", prop: "actualInventory", status: true },
        { label: "安全库存量（吨）", width: "120", prop: "safetyInventory", status: true },
        { label: "更新时间", width: "180", prop: "updateTime", status: true },
        { label: "操作", width: "200", prop: "", status: true },
      ],
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        stationId: [{ required: true, message: "请选择站点", trigger: "change" }],
        productId: [{ required: true, message: "请选择特材成品", trigger: "change" }],
        maxInventory: [{ required: true, message: "请输入最大库存量", trigger: "blur" }],
        actualInventory: [{ required: true, message: "请输入实际库存量", trigger: "blur" }],
        safetyInventory: [{ required: true, message: "请输入安全库存量", trigger: "blur" }],
      },
      lowrules1: {
        needNum: [{ required: true, message: "请输入需求量", trigger: "blur" }],
        needTime: [{ required: true, message: "请选择需求时间", trigger: "change" }],
      },
      lowrules2: {
        num: [{ required: true, message: "请输入库存盘点量", trigger: "blur" }],
        checkTime: [{ required: true, message: "请选择盘点时间", trigger: "change" }],
      },
      fromDate: {
        stationId: '',
        name: '',
        productId: '',
        rawInventoryWarehouseId: '',
        maxInventory: '',
        actualInventory: '',
        safetyInventory: '',
      },
      /**额外弹窗*/
      low: {},
      lowData: {
      },
      from3: {
        page: 1,
        pageSize: 10,
      },
      total2: 0,
      tableData1: [],
      addDialog1: false,
      addDialog2: false,
      addDialog3: false,
      pointList: [],
      productList: [],
      storeHouseList: [],
    };
  },
  async created() {
    this.pointList = await getUserAutList(true)
    /*存储拷贝的原初始化fromDate数据*/
    this.tableParam = { ...this.tableParam, stationId: "", productName: "", productType: '' };
    this.gettableData();
  },
  methods: {
    changeWareHouse (value) {
      const data = this.storeHouseList.find(item => item.rawWarehouseId === value);
      this.fromDate.actualInventory = data.currentInventory;
    },
    productListFun(id) {
      get_simple_organization({ id: id }).then((res) => {
        this.fromDate.companyId = res.data.id
        this.fromDate.company = res.data.name//查询站点所属公司
        productList({ page: 1, pageSize: 9999999, enable: 1, dataType: 2, companyId: this.fromDate.companyId }).then(res => {
          if (res.data.data.length == 0) {
            this.$message({
              message: '该站点公司无特材成品',
              type: 'warning'
            });
          }
          this.productList = res.data.data
        })
      });
      getStoreHouse({stationId: id}).then(res => {
        if (res.code === 1001) {
          this.storeHouseList = res.data;
          if (!res.data.length) {
            this.$message.warning('该站点下没有可用库存仓');
          }
        }
      })
    },
    /*重置数据,此方法必须存在，并且方发只重置条件*/
    init() {
      this.tableParam.stationId = "";
      this.tableParam.productName = "";
      this.tableParam.productType = "";
      this.gettableData();
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate(async (valid) => {
        if (valid) {
          const { data } = await get_simple_organization({ id: this.fromDate.stationId })
          this.fromDate.company = data.name
          this.fromDate.companyId = data.id
          let item = this.productList.find(res => (res.id == this.fromDate.productId))
          this.fromDate.productName = item.productName
          this.fromDate.productType = item.type
          _this.submitFromDate();
        } else {
          return false;
        }
      });
    },
    open(key, low) {

      this.low = this.copy(low);
      if (key == 'addDialog3') {
        this.from3.dataId = low.id
        this.getarr3()
      } else {
        setTimeout(() => {
          this.$refs[key].clearValidate();
        }, 10)
      }
      this[key] = true;
    },
    getarr3() {
      recordsqueryPage(this.from3).then(res => {
        this.tableData1 = res.data.data
        this.total2 = res.data.totalRows
        console.log(res.data)
      })
    },
    submit1(key) {
      let _this = this;
      _this.$refs[key].validate(async (valid) => {
        if (valid) {
          const { code } = await saveneed(this.low)
          if (code == 1001) {
            this.reset(key)
            this.$message({
              message: '保存成功',
              type: "success",
            });
          }
        } else {
          return false;
        }
      });
    },
    submit2(key) {
      let _this = this;
      _this.$refs[key].validate(async (valid) => {
        if (valid) {
          let data = {
            dataId: this.low.id,
            name: this.low.productName,
            num: this.low.num,
            checkTime: this.low.checkTime,
            type: this.low.productType,
          }
          const { code } = await stockTaking(data)
          if (code == 1001) {
            this.reset(key)
            this.$message({
              message: '保存成功',
              type: "success",
            });
            this.gettableData();
          }
        } else {
          return false;
        }
      });
    },
    reset(key) {
      this[key] = false;
      this.low = {}
    },
    safetyInventoryFun(size) {
      setTimeout(() => {
        if (size - this.fromDate.safetyInventory < 0) {
          this.$message({
            message: '安全库存量不能大于最大库存量！',
            type: 'warning'
          });
          this.fromDate.safetyInventory = size
        }
      }, 100)
    },
    sizeChange(value) {
      this.tableParam.pageSize = value;
      this.gettableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input,
.el-select,
.el-textarea {
  width: 240px;
}
/*
 <el-link class="table_link" :underline="false">查看配合比</el-link>*/
.table_link {
  margin: 0 10px;
  color: $bg_color;
  cursor: pointer;
}

.table_link:hover {
  opacity: 0.8;
}
</style>
